import requests
import base64
import time

def get_code():
    url = 'http://ec.yto.net.cn/api/user/vcode'
    headers = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Connection': 'keep-alive',
        'Cookie': 'UM_distinctid=17a9952f466163-03055d2e7b4905-7a697e6e-1fa400-17a9952f467cdd; Hm_lvt_986cf607d44c3333e9a678297feca67a=1626069990; Hm_lpvt_986cf607d44c3333e9a678297feca67a=1626069990; CNZZDATA1277696611=1958513112-1626066467-%7C1626066467; smsCode1=Mon%20Jul%2012%202021%2014:07:41%20GMT+0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)',
        'Host': 'ec.yto.net.cn',
        'Referer': 'http://ec.yto.net.cn/userlogin',
        'source': 'PC',
        'trace': '{"loginSource":"","terminalModel":""}',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67'
    }
    res = requests.get(url,headers=headers)
    result = res.json()
    randValue = result['randValue']
    i = result['base64Img']
    i = i.replace('data:image/png;base64,','')
    yzm = base64.b64decode(i)
    with open('yt.png','wb')as f:
        f.write(yzm)
        f.close()
    return randValue

def get_login(code,randValue):
    url = 'http://ec.yto.net.cn/api/user/login?randValue={}&code={}&username=19924237889&password=jfck2020'.format(randValue, code)
    headers = {
        'Origin': 'http://ec.yto.net.cn',
        'Referer': 'http://ec.yto.net.cn/userlogin',
        'source': 'PC',
        'trace': '{"loginSource":"12","terminalModel":"01"}',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67',
        'Cookie': 'UM_distinctid=17a9952f466163-03055d2e7b4905-7a697e6e-1fa400-17a9952f467cdd; Hm_lvt_986cf607d44c3333e9a678297feca67a=1626069990; Hm_lpvt_986cf607d44c3333e9a678297feca67a=1626069990; CNZZDATA1277696611=1958513112-1626066467-%7C1626066467'
    }
    data = {
        'randValue':randValue,
        'code':code,
        'username':19924237889,
        'password':'jfck2020'
    }
    res = requests.post(url,headers=headers,data=data)
    print(res.text)


def get_data(pageNo,f):
    # url = 'http://ec.yto.net.cn/api/order/queryRecieveOrderList?limit=10&pageNo={}&receiverPhone=19924237889&startTime=2021-07-08T16:00:00.000Z&endTime=2021-07-10T16:00:00.000Z'.format(pageNo)
    url = 'http://ec.yto.net.cn/api/order/queryRecieveOrderList?limit=10&pageNo={}&receiverPhone=19924237889&startTime=2021-07-04T16:00:00.000Z&endTime=2021-07-12T16:00:00.000Z'.format(pageNo)
    headers = {
        'Cookie': 'jwt-token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MjY4Mzc5MzMsInN1YiI6IntcInByb3ZpZGVyTmFtZVwiOlwiTG9naW5Ub2tlblByb3ZpZGVyXCIsXCJzb3VyY2VcIjpcIlVOS05PV05cIixcInJvbGVzXCI6W10sXCJkdXJhdGlvblwiOjEwMDgwLFwidXNlcklkXCI6OTEzMTQ4NzksXCJ1c2VybmFtZVwiOlwiMTk5MjQyMzc4ODlcIixcIm9wZW5JZExpc3RcIjpbXSxcIm9wZW5JZHNcIjpbXSxcImVuYWJsZWRcIjp0cnVlLFwiYWNjb3VudE5vbkxvY2tlZFwiOmZhbHNlLFwiY3JlZGVudGlhbHNOb25FeHBpcmVkXCI6ZmFsc2UsXCJhY2NvdW50Tm9uRXhwaXJlZFwiOmZhbHNlLFwiY2xhaW1zXCI6e1wiZHVyYXRpb25cIjoxMDA4MCxcInJvbGVzXCI6W10sXCJzb3VyY2VcIjpcIlVOS05PV05cIixcInByb3ZpZGVybmFtZVwiOlwiTG9naW5Ub2tlblByb3ZpZGVyXCIsXCJ1c2VybmFtZVwiOlwiMTk5MjQyMzc4ODlcIn19In0.7mhocdjX_3xlUc6dpqBpPAsnU1wHm3RgNH42gISDg-c',
        'Host': 'ec.yto.net.cn',
        'jwt-token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MjY4Mzc5MzMsInN1YiI6IntcInByb3ZpZGVyTmFtZVwiOlwiTG9naW5Ub2tlblByb3ZpZGVyXCIsXCJzb3VyY2VcIjpcIlVOS05PV05cIixcInJvbGVzXCI6W10sXCJkdXJhdGlvblwiOjEwMDgwLFwidXNlcklkXCI6OTEzMTQ4NzksXCJ1c2VybmFtZVwiOlwiMTk5MjQyMzc4ODlcIixcIm9wZW5JZExpc3RcIjpbXSxcIm9wZW5JZHNcIjpbXSxcImVuYWJsZWRcIjp0cnVlLFwiYWNjb3VudE5vbkxvY2tlZFwiOmZhbHNlLFwiY3JlZGVudGlhbHNOb25FeHBpcmVkXCI6ZmFsc2UsXCJhY2NvdW50Tm9uRXhwaXJlZFwiOmZhbHNlLFwiY2xhaW1zXCI6e1wiZHVyYXRpb25cIjoxMDA4MCxcInJvbGVzXCI6W10sXCJzb3VyY2VcIjpcIlVOS05PV05cIixcInByb3ZpZGVybmFtZVwiOlwiTG9naW5Ub2tlblByb3ZpZGVyXCIsXCJ1c2VybmFtZVwiOlwiMTk5MjQyMzc4ODlcIn19In0.7mhocdjX_3xlUc6dpqBpPAsnU1wHm3RgNH42gISDg-c',
        'Referer': 'http://ec.yto.net.cn/myOrder',
        'source': 'PC',
        'trace': '{"loginSource":"","terminalModel":""}',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67'
    }
    res = requests.get(url,headers=headers)
    result = res.json()
    for item in result['items']:
        print(item)
        f.write(str(item)+'\n')


if __name__ == "__main__":
    # randValue = get_code()
    # code = input('请输入验证码:')
    # get_login(code,randValue)
    f = open('yt_data_13_23.txt','a+',encoding='utf-8')
    for i in range(1,489):
        print('---------------------------'+str(i)+'-------------------------------')
        get_data(i,f)
        time.sleep(1.5)
    f.close()